//
//  WelfareExchangeIntegralViewController.m
//  quxiangtao
//
//  Created by xbmios on 2018/8/8.
//  Copyright © 2018年 shanhaijin. All rights reserved.
//

#import "WelfareExchangeIntegralViewController.h"
#import "WelfareIntegralRecordViewController.h"

@interface WelfareExchangeIntegralViewController ()

// 头部视图
@property (strong, nonatomic) UIView *topView;
// 头部蓝色背景
@property (strong, nonatomic) UIImageView *topBgIV;
// 白色视图
@property (strong, nonatomic) UIView *whiteContainerView;

@property (strong, nonatomic) UIView *currentIntegralView;
// 当前积分
@property (strong, nonatomic) UILabel *currentIntegralL;
// 当前积分数量
@property (strong, nonatomic) UILabel *currentIntegralNumberL;

@property (strong, nonatomic) UIView *canChangeIntegralView;
// 积分可兑换额度
@property (strong, nonatomic) UILabel *canChangeIntegralL;
// 积分可兑换额度数量
@property (strong, nonatomic) UILabel *canChangeIntegralNumberL;
// 分割线
@property (strong, nonatomic) UIView *verticalV;

// 兑换金额下的分割线
@property (strong, nonatomic) UIView *bottomLine;
// 兑换金额
@property (strong, nonatomic) UILabel *exchangeMoneyL;
// 兑换金额文本框
@property (strong, nonatomic) UITextField *exchangeMoneyTextField;

@property (strong, nonatomic) UIImageView *confirmIV;
@property (strong, nonatomic) UILabel *confirmL;

@end

@implementation WelfareExchangeIntegralViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = WhiteColor;
    
    [self createUI];
}

- (void)goBackAction {
    
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark -积分兑换明细
- (void)rigthBtnClick:(UIButton *)btn {
    
    NSLog(@"积分兑换明细");
    
   
    
}

#pragma mark -确认兑换
- (void)confirmGesture:(UIGestureRecognizer *)recognizer {
    
//     未输入兑换金额
    if (self.exchangeMoneyTextField.text.length <= 0) {

        NSLog(@"请输入兑换金额");
        [[KYAlertView sharedInstance] showAlertViewWithMessage:@"请输入兑换金额"];

        return;
    }
    
    if ([self.exchangeMoneyTextField.text floatValue] <= 0) {
        
        [[KYAlertView sharedInstance] showAlertViewWithMessage:@"请输入正确的兑换金额"];
        return;
    }
 
    NSString *str = nil;
    if ([self.dict[@"bean_quota"] isKindOfClass:[NSNull class]]) {
        
        str = @"0";
        
    }else {
        
        str = [NSString stringWithFormat:@"%@", self.dict[@"bean_quota"]];
    }
    
    // 输入的兑换金额大于可兑换金额
    if ([self.exchangeMoneyTextField.text floatValue] > [str floatValue]) {

        NSLog(@"兑换金额大于可兑换金额");
        [[KYAlertView sharedInstance] showAlertViewWithMessage:@"兑换金额大于可兑换金额"];

        return ;
    }
    
    //[CommonUserInfo sharedInstance].user_id
    NSString *par = [NSString stringWithFormat:@"{\"user_id\" : \"%@\", \"amount\" : \"%@\"}", [RSA encryptString:[CommonUserInfo sharedInstance].user_id publicKey:PublicKey], [RSA encryptString:self.exchangeMoneyTextField.text publicKey:PublicKey]];
    
    [PPNetworkHelper POST:@"user/Welfare/Beanexchange" parameters:@{@"json" : par} success:^(id responseObject) {
        
        NSLog(@"兑换金额：%@", responseObject);
        
        if ([responseObject[@"code"] integerValue] == 1) {
            
            [[KYAlertView sharedInstance] showAlertViewWithMessage:responseObject[@"msg"]];
            
            // 请求接口，成功之后返回控制器
            [self.navigationController popViewControllerAnimated:YES];
            
        }else {
            
            [[KYAlertView sharedInstance] showAlertViewWithMessage:responseObject[@"msg"]];
        }
        
    } failure:^(NSError *error) {
        
        if (error) {
            
            [[KYAlertView sharedInstance] showAlertViewWithMessage:Tip];
        }
    }];
    
    
    
    
}

- (void)createUI {
    
    [self.view addSubview:self.topView];
    [self.topView addSubview:self.topBgIV];
    [self.topView addSubview:self.whiteContainerView];
    [self.whiteContainerView addSubview:self.currentIntegralView];
    [self.whiteContainerView addSubview:self.canChangeIntegralView];
    [self.currentIntegralView addSubview:self.currentIntegralL];
    [self.currentIntegralView addSubview:self.currentIntegralNumberL];
    [self.canChangeIntegralView addSubview:self.canChangeIntegralL];
    [self.canChangeIntegralView addSubview:self.canChangeIntegralNumberL];
    [self.topView addSubview:self.verticalV];
    
    [self.view addSubview:self.exchangeMoneyL];
    [self.view addSubview:self.exchangeMoneyTextField];
    [self.view addSubview:self.bottomLine];
    
    [self.view addSubview:self.confirmIV];
    [self.view addSubview:self.confirmL];
    
    
    __weak typeof(self) weakSelf = self;
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.width.mas_equalTo(jisuan(750));
        CGFloat topHeight = (IS_IPHONE_X || IS_IPHONE_Xr || IS_IPHONE_Xs || IS_IPHONE_Xs_Max) ? jisuan(430) : jisuan(380);
        make.height.mas_equalTo(topHeight);
    }];
    
    [self.topBgIV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.mas_offset(0);
    }];
    
    [self.whiteContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.height.mas_equalTo(jisuan(200));
        make.left.mas_equalTo(weakSelf.topView.mas_left).with.offset(jisuan(20));
        make.right.mas_equalTo(weakSelf.topView.mas_right).with.offset(-jisuan(20));
        make.bottom.mas_equalTo(weakSelf.topView.mas_bottom).with.offset(-jisuan(20));
        
    }];
    
    CGFloat width = (WIDTH - 2 * jisuan(20)) / 2.0;

    [self.currentIntegralView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(weakSelf.whiteContainerView.mas_left);
        make.top.mas_equalTo(weakSelf.whiteContainerView.mas_top);
        make.bottom.mas_equalTo(weakSelf.whiteContainerView.mas_bottom);
        make.width.mas_equalTo(width);
        
    }];
    
    [self.canChangeIntegralView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(weakSelf.whiteContainerView.mas_right);
        make.top.mas_equalTo(weakSelf.whiteContainerView.mas_top);
        make.bottom.mas_equalTo(weakSelf.whiteContainerView.mas_bottom);
        make.width.mas_equalTo(width);
    }];
    
    [self.currentIntegralL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(weakSelf.currentIntegralView.mas_centerX);
        make.top.mas_equalTo(weakSelf.currentIntegralView.mas_top).with.offset(jisuan(40));
    }];
    
    [self.currentIntegralNumberL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(weakSelf.currentIntegralView.mas_left).with.offset(jisuan(20));
        make.right.mas_equalTo(weakSelf.currentIntegralView.mas_right).with.offset(-jisuan(20));
        make.top.mas_equalTo(weakSelf.currentIntegralL.mas_bottom).with.offset(jisuan(30));
    }];
    
    [self.canChangeIntegralL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.mas_equalTo(weakSelf.canChangeIntegralView.mas_centerX);
        make.top.mas_equalTo(weakSelf.canChangeIntegralView.mas_top).with.offset(jisuan(40));
        
    }];
    [self.canChangeIntegralNumberL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.canChangeIntegralView.mas_left).with.offset(jisuan(20));
        make.right.mas_equalTo(weakSelf.canChangeIntegralView.mas_right).with.offset(-jisuan(20));
        make.top.mas_equalTo(weakSelf.canChangeIntegralL.mas_bottom).with.offset(jisuan(30));
        
    }];
    
    
    [self.verticalV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.height.mas_equalTo(jisuan(60));
        make.width.mas_equalTo(jisuan(2));
        make.centerY.mas_equalTo(weakSelf.whiteContainerView.mas_centerY);
        make.centerX.mas_equalTo(weakSelf.whiteContainerView.mas_centerX);
        
    }];
    
    [self.bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.topView.mas_bottom).with.offset(jisuan(120));
        make.left.mas_equalTo(weakSelf.view.mas_left).with.offset(jisuan(20));
        make.right.mas_equalTo(weakSelf.view.mas_right).with.offset(-jisuan(20));
        make.height.mas_equalTo(jisuan(2));
        
    }];
    [self.exchangeMoneyL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(weakSelf.bottomLine.mas_top).with.offset(-jisuan(36));
        make.left.mas_equalTo(weakSelf.view.mas_left).with.offset(jisuan(20));
        make.width.mas_equalTo(jisuan(140));
        make.height.mas_equalTo(jisuan(40));

    }];
    [self.exchangeMoneyTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.exchangeMoneyL.mas_right).with.offset(jisuan(20));
        make.centerY.mas_equalTo(weakSelf.exchangeMoneyL.mas_centerY);
        make.height.mas_equalTo(jisuan(40));
        make.right.mas_equalTo(weakSelf.view.mas_right).with.offset(-jisuan(20));
        
    }];
    [self.confirmIV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.mas_equalTo(weakSelf.view.mas_centerX);
        make.bottom.mas_equalTo(weakSelf.view.mas_bottom).with.offset(-jisuan(66));
        make.width.mas_equalTo(jisuan(550));
        make.height.mas_equalTo(jisuan(106));
    }];
    [self.confirmL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.mas_equalTo(weakSelf.confirmIV.mas_centerX);
        make.centerY.mas_equalTo(weakSelf.confirmIV.mas_centerY).with.offset(-jisuan(4));
    }];
}

#pragma mark -lazy
- (UIView *)topView {
    
    if (!_topView) {
        
        _topView = [[UIView alloc] init];
        _topView.backgroundColor = WhiteColor;
    }
    
    return _topView;
}

- (UIImageView *)topBgIV {
    
    if (!_topBgIV) {
        
        _topBgIV = [[UIImageView alloc] init];
        _topBgIV.image = [UIImage imageNamed:@"financialHeaderbg"];
    }
    
    return _topBgIV;
}

- (UIView *)whiteContainerView {
    
    if (!_whiteContainerView) {
        
        _whiteContainerView = [[UIView alloc] init];
        _whiteContainerView.backgroundColor = WhiteColor;
        
        _whiteContainerView.layer.cornerRadius = jisuan(10);
        _whiteContainerView.layer.masksToBounds = YES;
    
    }
    
    return _whiteContainerView;
}

- (UILabel *)currentIntegralL {
    
    if (!_currentIntegralL) {
        
        _currentIntegralL = [[UILabel alloc] init];
        _currentIntegralL.textColor = Color999999;
        _currentIntegralL.font = Font(24);
        _currentIntegralL.textAlignment = NSTextAlignmentCenter;
        _currentIntegralL.text = @"当前积分";
        
    }
    
    return _currentIntegralL;
}

- (UILabel *)currentIntegralNumberL {
    
    if (!_currentIntegralNumberL) {
        
        _currentIntegralNumberL = [[UILabel alloc] init];
        _currentIntegralNumberL.textColor = Color1f89ff;
        _currentIntegralNumberL.font = Font(60);
        _currentIntegralNumberL.textAlignment = NSTextAlignmentCenter;
        NSString *str = [NSString stringWithFormat:@"%@", self.dict[@"user_bean"]];
        _currentIntegralNumberL.text = str;
        
    }
    
    return _currentIntegralNumberL;
}

- (UILabel *)canChangeIntegralL {
    
    if (!_canChangeIntegralL) {
        
        _canChangeIntegralL = [[UILabel alloc] init];
        _canChangeIntegralL.textColor = Color999999;
        _canChangeIntegralL.font = Font(24);
        _canChangeIntegralL.textAlignment = NSTextAlignmentCenter;
        _canChangeIntegralL.text = @"趣豆";
        
    }
    
    return _canChangeIntegralL;
}

- (UILabel *)canChangeIntegralNumberL {
    
    if (!_canChangeIntegralNumberL) {
        
        _canChangeIntegralNumberL = [[UILabel alloc] init];
        _canChangeIntegralNumberL.textColor = Colorff0000;
        _canChangeIntegralNumberL.font = Font(60);
        _canChangeIntegralNumberL.textAlignment = NSTextAlignmentCenter;
        
        
        NSString *str = nil;
        
        if ([self.dict[@"bean_quota"] isKindOfClass:[NSNull class]]) {
            
            str = @"0";
            
        }else {
            
            str = [NSString stringWithFormat:@"%@", self.dict[@"bean_quota"]];
        }
        
        _canChangeIntegralNumberL.text = str;
    
        
    }
    
    return _canChangeIntegralNumberL;
}

- (UIView *)verticalV {
    
    if (!_verticalV) {
        
        _verticalV = [[UIView alloc] init];
        _verticalV.backgroundColor = Colordddddd;
    }
    
    return _verticalV;
}

- (UIView *)currentIntegralView {
    
    if (!_currentIntegralView) {
        
        _currentIntegralView = [[UIView alloc] init];
        _currentIntegralView.backgroundColor = WhiteColor;
    }
    
    return _currentIntegralView;
}
- (UIView *)canChangeIntegralView {
    
    if (!_canChangeIntegralView) {
        
        _canChangeIntegralView = [[UIView alloc] init];
        _canChangeIntegralView.backgroundColor = WhiteColor;
    }
    
    return _canChangeIntegralView;
}

- (UIView *)bottomLine {
    
    if (!_bottomLine) {
        
        _bottomLine = [[UIView alloc] init];
        _bottomLine.backgroundColor = Colordddddd;
    }
    
    return _bottomLine;
}

- (UILabel *)exchangeMoneyL {
    
    if (!_exchangeMoneyL) {
        
        _exchangeMoneyL = [[UILabel alloc] init];
        _exchangeMoneyL.textColor = Color333333;
        _exchangeMoneyL.font = Font(24);
        _exchangeMoneyL.textAlignment = NSTextAlignmentLeft;
        _exchangeMoneyL.text = @"兑换金额";
        
    }
    
    return _exchangeMoneyL;
}

- (UITextField *)exchangeMoneyTextField {
    
    if (!_exchangeMoneyTextField) {
        
        _exchangeMoneyTextField = [[UITextField alloc] init];
        _exchangeMoneyTextField.textColor = Color333333;
        _exchangeMoneyTextField.font = Font(24);
        _exchangeMoneyTextField.textAlignment = NSTextAlignmentLeft;
        _exchangeMoneyTextField.placeholder = @"请输入需要兑换的金额";
        _exchangeMoneyTextField.keyboardType = UIKeyboardTypeDecimalPad;
    }
    
    return _exchangeMoneyTextField;
}

- (UIImageView *)confirmIV {
    
    if (!_confirmIV) {
        
        _confirmIV = [[UIImageView alloc] init];
        _confirmIV.contentMode = UIViewContentModeScaleAspectFit;
        _confirmIV.image = [UIImage imageNamed:@"redBtnBG"];
        _confirmIV.userInteractionEnabled = YES;
        
        UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(confirmGesture:)];
        [_confirmIV addGestureRecognizer:gesture];
        
    }
    
    return _confirmIV;
}

- (UILabel *)confirmL {
    
    if (!_confirmL) {
        
        _confirmL = [[UILabel alloc] init];
        _confirmL.text = @"确认兑换";
        _confirmL.textColor = WhiteColor;
        _confirmL.textAlignment = NSTextAlignmentCenter;
        _confirmL.font = Font(32);
        
    }
    
    return _confirmL;
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}


@end
